package com.bravo.web.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: Yuyong
 * @create: 2019-09-06 09:55
 **/

@Slf4j
@Component
public class WebFilter extends ZuulFilter {


    @Override
    public String filterType() {
        return "pre";//前置过滤器
    }

    @Override
    public int filterOrder() {
        return 0;//优先级为0,数字越大,优先级越低
    }

    @Override
    public boolean shouldFilter() {
        return true;//是否执行过滤器,此处为true,说明需要过滤
    }

    @Override
    public Object run() throws ZuulException {
        log.info("zuul过滤器");
        //向header中添加令牌
        RequestContext requestContext = RequestContext.getCurrentContext();
        //获取header
        HttpServletRequest request = requestContext.getRequest();
        String authorization = request.getHeader("Authorization");
        if (StringUtils.isEmpty(authorization)){
            requestContext.addZuulRequestHeader("Authorization",authorization);
        }
        return null;
    }
}
